package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.enums.model.OrderStatus;
import com.atguigu.gmall.enums.model.PaymentStatus;
import com.atguigu.gmall.enums.model.PaymentType;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-03-13 15:34
 */
@Slf4j
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private RabbitService rabbitService;


    /**
     * 只要展示支付宝支付页面,需要在本地存储一条本地交易记录
     *
     * @param paymentInfo
     * @param paymentType
     */
    @Override
    public void savePaymentInfo(PaymentInfo paymentInfo, String paymentType) {
        //1.根据订单编号,支付方式 查询交易记录是否存在
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, paymentInfo.getOutTradeNo());
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        int count = this.count(queryWrapper);
        if (count != 0) {
            return;
        }

        //2.如果交易记录不存在,则新增本地交易记录
        this.save(paymentInfo);
    }


    /**
     * 根据订单编号+付款方式查询本地交易记录
     *
     * @param outTradeNo
     * @param payType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String payType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, outTradeNo);
        queryWrapper.eq(PaymentInfo::getPaymentType, payType);
        return this.getOne(queryWrapper);
    }




    /**
     * 更新本地交易记录表
     *
     * @param paymentInfo
     * @param paramsMap
     */
    @Override
    public void paySuccess(PaymentInfo paymentInfo, Map<String, String> paramsMap) {
        //1.设置本地交易记录表中字段值
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramsMap.toString());
        this.updateById(paymentInfo);

        //2.发送消息到MQ 异步通知订单微服务修改订单支付状态
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, paymentInfo.getOrderId());

    }


    /**
     * 根据订单ID+支付方式查询本地交易记录
     * @param orerId
     * @param payType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(Long orerId, String payType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId, orerId);
        queryWrapper.eq(PaymentInfo::getPaymentType, payType);
        return this.getOne(queryWrapper);
    }

}
